package com.travelsupport.controller;



import java.util.ArrayList;
import java.util.List;

import com.travelsupport.myobject.CatalogObj;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DbController  extends  SQLiteOpenHelper{
	
	private static final String DATABASE_NAME = "traveldb";
	private static final int DATABASE_VERSION = 1;
	private static final String TAG = "DbController";
	
	private Context mCxt ;
	
	private static  int currentVer ;//= 1;  //??????
	
  //  public SQLiteOpenHelper dbHelper = null ;//= new SQLiteOpenHelper();
    public SQLiteDatabase db = null ;
	public CatalogObj catalog =  null ;
	
	private static DbController mInstance = null;
	
	@Override
	public void onCreate(SQLiteDatabase db) {   //is called by framework if Database no exist
		// TODO Auto-generated method stub
	//	db.execSQL("");
		Log.i(TAG,"onCreate");
		
	//	openDb();
		
		Log.i(TAG,"onCreate");
	//	openDb();
		selectCatalog();
		
	}
	
	public DbController(Context context) {		
		
	super(context,DATABASE_NAME,null,DATABASE_VERSION);
	Log.i(TAG,"DbController");
	//selectCatalog();
	//	this.mCxt = context;
	//	super(context,DATABASE_NAME,null,null);
	}
	
	public SQLiteDatabase openDb() throws SQLException {
		//
	//	dbHelper = new DbController(getContext());
		Log.i(TAG,"openDb()") ;
		
    //	return this.getWritableDatabase();
		
    	//return true;
		
		db = SQLiteDatabase.openDatabase("data/data/com.travelsupport.ui/databases/traveldb", null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		if(db == null) { Log.i(TAG,"database not exist"); }
		else Log.i(TAG,"data exist");
		
		return db;
    }
	
	public void closeDb() throws SQLException{
		
		this.db.close();
		
	}
	
	public void selectMyFavorite(){
		
	}
	
	public void updateLocation(){
		
	}
	
	public void updateLocations(){
		
	}
	
	/*public static DbController getInstance(Context ctx) {
		if(mInstance == null){
			mInstance = new DbController(ctx.getApplicationContext());
		}
		
		return mInstance;
	}
	*/
	public void selectCatalog(){
		Log.i(TAG,"selectCatalog");
		
		SQLiteDatabase db = openDb() ;
		
		List<CatalogObj> catalogs = new ArrayList<CatalogObj>();
		
		Cursor cursor = db.rawQuery("SELECT * FROM tbCatalogs",null);
		
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			catalog  = cursorToCatalog(cursor);
			catalogs.add(catalog);
			
			Log.i(TAG,"name = "+catalog.getName());
			
		}
		
		cursor.close();
		
		
	}
	
	private CatalogObj cursorToCatalog(Cursor cursor){
		CatalogObj catalog = new CatalogObj();
		//catalog.setIcon(cursor.getString(cursor.getColumnIndex("icon")));
		catalog.setName(cursor.getColumnName(cursor.getColumnIndex("name")));
		
		return catalog ;
		
	//	cursor.
		
	}
	
	public void selectSubCatalog(){
		
	}
	
	public void selectLocations(){
		
	}
	
	public void selectPrices(){
		
	}
	
    public void selectQuanlities(){
		
	}
    
    public void filterLocation(){
		
	}
    
    public void selectLocation(){
    	
    }
    
    public void insertMyFavorite(){
    	
    }
	
    public boolean checkVersion(){
    	if(currentVer == getDbVersion(db));
    	return true;
    }
   
	public SQLiteDatabase createDb(Context context){
		 return this.getWritableDatabase();
		//SQLiteDatabase db = dbHelper.getWritableDatabase();
	}
	
	public void selectCurrency(){
		
	}
	
	 /* PhuongCT insert the function */
	 public Cursor selectQuery(String query) {
	  Cursor c1 = null;
	  SQLiteDatabase db = openDb() ;
		  	c1 = db.rawQuery(query, null);
	  	return c1;
	 }
	public boolean checkDbExist(){
		boolean isExist = false;
		
		return isExist;
	}
	
	public void copyDatabase(){
		
	}
	
	public void joinFiles(){
		
	}
	
	public int getDbVersion(){
		int version = 1;
		
		return version;
	}
	
	public void setDbVersion(int version){
		
		currentVer = version;
	}
	
     @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    	// TODO Auto-generated method stub
    	
    }
	 public int  getDbVersion(SQLiteDatabase db){
	    	//int lastestVersion = 0;
	  
	    	return db.getVersion();
	    	
	    }
	    
	    
	    
	    
	    
	   /* public Cursor getAll(String tableName){
	    
	   // String query = "SELECT * FROM "+ tableName;
	    
	    return(db.rawQuery("SELECT * FROM "+tableName,null)); 
	    }*/
	    
	    
	    
}
